﻿using System;
using System.Windows;
using ControleRepresentanteSync.Application.SimpleSync;
using ControleRepresentanteSync.Logic.Models;

namespace ControleRepresentanteSync.View.SimpleSync
{
    internal abstract class SearchableSelectionViewModel<TContentViewModel, TModel, TItem, TContract> : SimpleSyncSelectionViewModel<TContentViewModel, TModel, TItem, TContract>, ISearchableSelectionViewModel<TContentViewModel, TModel, TItem, TContract>
        where TContentViewModel : ISearchableContentViewModel<TModel, TItem, TContract>
        where TItem : ISimpleSycItem<TContract>
        where TModel : Searchable
    {
        public static readonly DependencyProperty SearchRazaoSocialProperty =
            DependencyProperty.Register("SearchRazaoSocial", typeof(string), typeof(SearchableSelectionViewModel<TContentViewModel, TModel, TItem, TContract>), new PropertyMetadata(OnSearchChanged));
        public static readonly DependencyProperty SearchNomeFantasiaProperty =
            DependencyProperty.Register("SearchNomeFantasia", typeof(string), typeof(SearchableSelectionViewModel<TContentViewModel, TModel, TItem, TContract>), new PropertyMetadata(OnSearchChanged));
        public static readonly DependencyProperty SearchCidadeProperty =
            DependencyProperty.Register("SearchCidade", typeof(string), typeof(SearchableSelectionViewModel<TContentViewModel, TModel, TItem, TContract>), new PropertyMetadata(OnSearchChanged));
        public static readonly DependencyProperty SearchBairroProperty =
            DependencyProperty.Register("SearchBairro", typeof(string), typeof(SearchableSelectionViewModel<TContentViewModel, TModel, TItem, TContract>), new PropertyMetadata(OnSearchChanged));

        private static void OnSearchChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SearchableSelectionViewModel<TContentViewModel, TModel, TItem, TContract>)d).FireSearchChanged();
        }

        protected SearchableSelectionViewModel(string title, string description)
            : base(title, description)
        {
        }

        private void FireSearchChanged()
        {
            if (SearchChanged != null)
                SearchChanged(this, EventArgs.Empty);
        }

        public string SearchRazaoSocial
        {
            get { return (string)GetValue(SearchRazaoSocialProperty); }
            set { SetValue(SearchRazaoSocialProperty, value); }
        }

        public string SearchNomeFantasia
        {
            get { return (string)GetValue(SearchNomeFantasiaProperty); }
            set { SetValue(SearchNomeFantasiaProperty, value); }
        }

        public string SearchCidade
        {
            get { return (string)GetValue(SearchCidadeProperty); }
            set { SetValue(SearchCidadeProperty, value); }
        }

        public string SearchBairro
        {
            get { return (string)GetValue(SearchBairroProperty); }
            set { SetValue(SearchBairroProperty, value); }
        }

        public event EventHandler SearchChanged;
    }
}